<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/13 0013
 * Time: 上午 11:23
 */
class GoodsController extends Controller{

    function  filters(){
        return array(
            'accessControl',
        );
    }
    //show add update del 等方法 进行访问需要用户登录系统
    //为具体方法被访问设置条件

    function  accessRules(){
        return array(
            //用户访问扩展
            //add方法-可以无条件访问
            array(
                'allow', //允许访问
                'actions'=>array('add'),
                'users'=>array('*'),//所有用户
            ),
            //指定用户 可以删除信息
            array(
                'allow',//允许访问
                'actions'=>array('del'),
                'users'=>array('admin'),//指定用户
            ),
            //匿名用户访问 操作 ?
            array(
                'allow',//允许访问
                'actions'=>array('update'),
                'users'=>array('?'),//匿名用户 ?id = 12
            ),
            // 必须是登录系统的用户来访问
            array(
                'allow',//允许访问
                'actions'=>array('show','add','update'),
                'users'=>array('@'), //登录系统用户
            ),
            //所有用户禁止访问//上面的方法之外的
            array(
                'deny',//拒绝访问
                'users'=>array('*'), //所有用户
            ),
        );
    }


    function actionShow1(){
        //var_dump(Yii::App()->db);
        //通过模型model实现数据表信息查询
        //产生模型model对象
        $goods_model = Goods::model();

//        $goods_info = $goods_model->find();//1.查找一条数据
//        $goods_infos = $goods_model->findAll();//2.查找多条数据
        //遍历输出
//        foreach($goods_infos as $v){
//            echo $v ->goods_name,$v->goods_price."<br>";
//        }
        //3.通过具体sql 语句获取商品信息
//        $sql = "select * from {{goods}} limit 10";
        $sql = "select * from {{goods}}";
        $goods_infos = $goods_model -> findAllBySql($sql);

//        var_dump($goods_infos);

        $this->renderPartial('show',array('goods_infos'=>$goods_infos));
    }

    //建立测试方法，实现数据分页
    function actionShow(){
        $goods_model = Goods::model();
        //分页
        $cnt = $goods_model ->count();
        $per = 6;

        $page = new Pagination($cnt,$per);
        //拼装sql语句
        $sql = "select * from {{goods}} ".$page ->limit;
        $goods_infos = $goods_model->findAllBySql($sql);

        $page_list = $page->fpage(array(3,4,5,6,7,8));

//        $goods_infos = $goods_model->findAll();
        $this->renderPartial('show',array('goods_infos'=>$goods_infos,'page_list'=>$page_list));
    }

    function actionAdd(){
        $goods_model = new Goods();//调用save 会执行insert
//        var_dump($_POST);
        if(isset($_POST['Goods'])){
            //我们把从表单提交过来的数据赋予$goods_model模型里面
//            $goods_model->goods_name = $_POST['Goods']['goods_name'];
            foreach($_POST['Goods'] as $k => $v){
                $goods_model-> $k = $v;
            }
            $goods_model->goods_create_time = time();

            //调用save()方法实现数据添加
            if($goods_model->save()){
                Yii::app()->user->setFlash('success','添加商品成功');
//                echo '保存成功';
//                $this->redirect(array('Goods/show'));
                $this->redirect('./index.php?r=houtai/goods/show');
            }else{
                echo '保存失败';
//                $this->redirect(array('Goods/add','id'=>1));
            }
        }
        $this->renderPartial('add',array('goods_model'=>$goods_model));
    }

    function actionUpdate($id){
//        $goods_model = new Goods();//调用save 会执行insert
        $goods_model = Goods::model();//调用save 会执行update
        $goods_info = $goods_model->findByPk($id);//findByPk()通过组件查询数据信息
//        var_dump($goods_info);

        if(isset($_POST['Goods'])){
            foreach($_POST['Goods'] as $k => $v){
                $goods_info-> $k = $v;
            }
            if($goods_info->save()){
                $this->redirect('./index.php?r=houtai/goods/show');
            }
        }

        $this->renderPartial('update',array('goods_model'=>$goods_info));
    }

    function actionDel($id){
        $goods_model = Goods::model();//获得数据模型对象
        $goods_info = $goods_model->findByPk($id);//获得被删除的商品对象
        if($goods_info->delete()){
            $this->redirect('./index.php?r=houtai/goods/show');
        }

    }



}